package com.cdream.basic.algorithm.leetcode.twoPointer;

import java.util.HashSet;
import java.util.Set;

/**
 * @author cdream
 * @date 5/15/21
 */
public class Question345 {
    public static void swap(char[] arr, int i, int j) {
        char temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public String reverseVowels(String s) {
        Set<Character> volwels = new HashSet<>();
        volwels.add('a');
        volwels.add('e');
        volwels.add('i');
        volwels.add('o');
        volwels.add('u');
        volwels.add('A');
        volwels.add('E');
        volwels.add('I');
        volwels.add('O');
        volwels.add('U');
        char[] chars = s.toCharArray();
        int start = 0;
        int end = s.length() - 1;
        while (end > start) {
            if (volwels.contains(s.charAt(start)) && volwels.contains(s.charAt(end))) {
                swap(chars, end--, start++);
            } else if (volwels.contains(s.charAt(start)) && !volwels.contains(s.charAt(end))) {
                end--;
            } else if (!volwels.contains(s.charAt(start)) && volwels.contains(s.charAt(end))) {
                start++;
            } else {
                end--;
                start++;
            }
        }
        return new String(chars);
    }
}
